﻿using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.Kernel.Events;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using LiveChartsCore.Kernel;

using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using System.Runtime.Serialization;
using LiveChartsCore.SkiaSharpView.SKCharts;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView.VisualElements;

namespace TCOChart
{
    public partial class FormChart : Form
    {
        public ObservableCollection<ObservableValue> ObservableValues { get; set; }
        public ISeries[] Series { get; set; }
        private ColumnSeries<ObservableValue> m_input_columnSeries { get; set; }
        private ColumnSeries<ObservableValue> m_onepass_columnSeries { get; set; }
        private LineSeries<ObservableValue> m_yield_line { get; set; }
        private StepLineSeries<ObservableValue> m_time_stepline { get; set; }

        public FormChart()
        {
            InitializeComponent();

            Init();
        }

        public void update(string jsonStr)
        {
            try
            {
                var processor = DataProcessor.FromJson(jsonStr);
                m_input_columnSeries.Values = processor.Input.Select(value => new ObservableValue(value)).ToList();
                m_onepass_columnSeries.Values = processor.OnePass.Select(value => new ObservableValue(value)).ToList();
                m_yield_line.Values = processor.Yieid.Select(value => new ObservableValue(value)).ToList();
                m_time_stepline.Values = processor.Time.Select(value => new ObservableValue(int.Parse(value))).ToList();

                var axisX = cartesianChart1.XAxes.FirstOrDefault();
                if (axisX != null)
                {
                    //axisX.Labels = processor.Time;
                    axisX.LabelsRotation = 0; // 设置标签旋转角度
                    axisX.MaxLimit = processor.Time.Count - 1;
                }
                cartesianChart1.Title = new LabelVisual
                {
                    Text = processor.context,
                    TextSize = 20,
                    //BackgroundColor = new LvcColor(20, 200, 200)
                };
                cartesianChart1.Update();
            }
            catch { }
        }

        private void Init()
        {
            try
            {
                m_input_columnSeries = new ColumnSeries<ObservableValue>()
                {
                    Name = "input",
                    Values = new ObservableCollection<ObservableValue>(),
                    ScalesYAt = 0,  // 以哪一个Y轴为准，0是Y轴的索引
                    DataLabelsPosition = LiveChartsCore.Measure.DataLabelsPosition.Top,  // 数值显示位置
                    DataLabelsPaint = new SolidColorPaint(new SKColor(45, 45, 45)),  // 设置显示在图形中的数值颜色
                    //IsHoverable = false,  // 不显示悬停消息
                    //Stroke = new SolidColorPaint(new SKColor(0, 0, 0)){StrokeThickness = 2 },  // 用于显示图形的边框属性  这里边框显示黑色宽度为2
                    //Fill = new SolidColorPaint(new SKColor(255, 0, 0, 128)),  // 设置填充颜色，这里是半透明红色
                    IgnoresBarPosition = true,  // 忽略位置显示，就是重叠
                    MaxBarWidth = 30,  // 设置最大宽度
                };
               
                m_onepass_columnSeries = new ColumnSeries<ObservableValue>()
                {
                    Name = "一次PASS",
                    Values = new ObservableCollection<ObservableValue>(),
                    ScalesYAt = 0,
                    DataLabelsPosition = LiveChartsCore.Measure.DataLabelsPosition.Top,  // 数值显示位置
                    DataLabelsPaint = new SolidColorPaint(new SKColor(45, 45, 45)),  // 设置显示在图形中的数值颜色
                    IgnoresBarPosition= true,
                    MaxBarWidth=20,
                    Stroke = null,
                    
                };
                m_yield_line = new LineSeries<ObservableValue>()
                {
                    Name = "直通率(%)",
                    Values = new ObservableCollection<ObservableValue>(),
                    ScalesYAt = 1,
                    DataLabelsPosition = LiveChartsCore.Measure.DataLabelsPosition.Top,  // 数值显示位置
                    DataLabelsPaint = new SolidColorPaint(new SKColor(45, 45, 45)),  // 设置显示在图形中的数值颜色
                    DataLabelsFormatter = value => (value.Coordinate.PrimaryValue / 100).ToString("P"),
                    LineSmoothness = 1,  // 设置曲线的平滑性， 0--1，由直到弯曲
                    Fill = new SolidColorPaint(new SKColor(20, 200, 200, 128)),  // 设置图形填充，折线的下部分进行填充
                    //Stroke = null,
                    //GeometryStroke = null,  //折线上几何图形边框设置
                    //GeometryFill = null,  // 折线上几何图形填充
                };
                m_time_stepline = new StepLineSeries<ObservableValue>()
                {
                    Name = "time",
                    Values = new ObservableCollection<ObservableValue>(),
                    ScalesYAt = 0,
                    //DataLabelsFormatter = value => (value.Coordinate.PrimaryValue / 100).ToString("P"),
                };

                var stepLineSeries = new StepLineSeries<double>()
                {
                    Values = [4,2,6,8],
                    Fill = null,
                    Name = "stepLine",
                };
                

                Series = [m_input_columnSeries, m_onepass_columnSeries, m_yield_line, m_time_stepline, stepLineSeries];

                cartesianChart1.Series = Series;
                cartesianChart1.XAxes = [
                    new Axis{
                    TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),  // 刻度线颜色
                    TicksAtCenter = true,  // 是否位于中间
                    MinStep = 1,
                    SeparatorsPaint = new SolidColorPaint(new SKColor(200, 200, 200)),

                    
                    CrosshairLabelsBackground = SKColors.DarkOrange.AsLvcColor(),
                    CrosshairLabelsPaint = new SolidColorPaint(SKColors.DarkRed),
                    CrosshairPaint = new SolidColorPaint(SKColors.DarkOrange, 1),
                    Labeler = value => value.ToString("N3")
                }];
                cartesianChart1.YAxes = [
                    new Axis{
                    TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),  // 刻度线颜色
                    TicksAtCenter = true,  // 是否位于中间
                    //SeparatorsPaint = new SolidColorPaint(new SKColor(200, 200, 200)),
                    Position = LiveChartsCore.Measure.AxisPosition.Start,
                    MinLimit = 0,
                    },
                    new Axis{
                    //TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),  // 刻度线颜色
                    //TicksAtCenter = true,  // 是否位于中间
                    //SeparatorsPaint = new SolidColorPaint(new SKColor(200, 200, 200)),
                    Position = LiveChartsCore.Measure.AxisPosition.End,
                    Labeler = (value) => (value/100).ToString("P"),
                    //MinStep = 1,  // 最小步长
                    MinLimit = 0,  // 最小范围
                    //Name = "P"
                    
                }
                ];

                cartesianChart1.ZoomMode = LiveChartsCore.Measure.ZoomAndPanMode.X;
                cartesianChart1.LegendPosition = LiveChartsCore.Measure.LegendPosition.Bottom;
                cartesianChart1.LegendTextSize = 18;
                cartesianChart1.Dock = DockStyle.Fill;
                //cartesianChart1.Font = new Font(new System.Drawing.FontFamily("Microsoft YaHei"));
            }
            catch (Exception ex)
            { }
        }

        private void cartesianChart1_Load(object sender, EventArgs e)
        {
            string jsonStr2 = @"
               {""Context"":""Alerting..."",""Input"":[10,1,2,22,2,2,0],""OnePass"":[9,1,1,2,2,6,4,5],""Yieid"":[50,30, 25.3,0],""Time"":[""8"",""9"",""10"",""11"",""12"",""13"",""14"",""15"",""16"",""17"",""18"",""19""]} ";

            update(jsonStr2);
        }
    }


    public class DataProcessor
    {
        public List<int> Input { get; set; }
        public List<int> OnePass { get; set; }
        public List<double> Yieid { get; set; }
        public List<string> Time { get; set; }
        public string context { get; set; }

        public DataProcessor(List<int> input, List<int> onePass, List<double> yieid, List<string> time, string context)
        {
            Input = input;
            OnePass = onePass;
            Yieid = yieid;
            Time = time;
            this.context = context;
        }

        public void Display()
        {
            Console.WriteLine("Input: " + string.Join(", ", Input));
            Console.WriteLine("OnePass: " + string.Join(", ", OnePass));
            Console.WriteLine("Yieid: " + string.Join(", ", Yieid));
            Console.WriteLine("Time: " + string.Join(", ", Time));
        }

        public static DataProcessor FromJson(string jsonStr)
        {
            var data = JObject.Parse(jsonStr);

            var input = data["Input"].ToObject<List<int>>();
            var onePass = data["OnePass"].ToObject<List<int>>();
            var yieid = data["Yieid"].ToObject<List<double>>();
            var time = data["Time"].ToObject<List<string>>();
            var context = data["Context"].ToString();

            return new DataProcessor(input, onePass, yieid, time, context);
        }
    }
}
